<?php
namespace Common\Model;
use Think\Model;

/**
 * 记录账户金额变更操作模型
 * targetuserid     相关用户id
 * userid			目标用户id
 * money  			  变更金额
 * changetype		  变更类型
 * 
 */
class DetailedBonusModel extends Model {

	public function changemoney($data){		
		$data['create_time'] =time();     //创建时间
		$data['status'] =1;
		$field=$data['field'];
		if(empty($field))
		{
		    $field="money";
		}
		$money=$data['money'];//变更金额
		//获取目标用户最后一条流水记录
		$map['userid']= $data['userid']; 
		$lastmoney= $this->where($map)->order('id desc')->getField('balance');
		if($lastmoney){
		$data['balance']=$lastmoney+$money;
		}else{
			$info=M('User')->where(array('id'=>$data['userid']))->getField($field);
			$data['balance']=$info+$money;
		}
		//$this->startTrans();
		$res=$this->add($data);
		//exit;
		$res2=D('User')->where(array('id'=>$data['userid']))->setInc($field,$money);
		if($res && $res2){
			$this->commit();//成功则提交
			//记录公司财务即拨出率只有奖金流水有此项
			return $res;
		}else{
			$this->rollback();//不成功，则回滚
			return false;
		}
	}
}